package com.gzhryc.email;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.email.dao.InsideEmailAttachmentDao;
import com.gzhryc.email.dao.InsideEmailDao;
import com.gzhryc.email.dao.InsideEmailUserDao;
import com.gzhryc.email.dao.db.InsideEmail;
import com.gzhryc.email.dao.db.InsideEmailAttachment;
import com.gzhryc.email.dao.db.InsideEmailUser;
import com.gzhryc.email.dao.enums.InsideEmailUserEnum;
import com.gzhryc.email.dao.models.InsideEmailEntity;
import com.gzhryc.email.dto.InsideEmailSearch;
import com.gzhryc.email.dto.InsideEmailSendUser;
import com.gzhryc.jdbc.Conditions;

/**
 * 网站内部邮件服务类
 * @author 张浩
 * @version 2016-11-15
 */
public class InsideEmailService {

	static InsideEmailDao insideEmailDao = new InsideEmailDao();
	static InsideEmailAttachmentDao insideEmailAttachmentDao = new InsideEmailAttachmentDao();
	static InsideEmailUserDao insideEmailUserDao = new InsideEmailUserDao();
	
	public static List<InsideEmailEntity> findEntityBySearch(InsideEmailSearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = new Conditions();
		condition.and().likeBoth("t.title", search.getTitle());
		condition.and().is("t.type", search.getType());
		condition.and().is("t.status", search.getStatus());
		condition.desc("t.create_date");
		try {
			return insideEmailDao.findEntity(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据查询条件统计网站内部邮件数量
	 * @param search
	 * @return
	 */
	public static Long countEntityBySearch(InsideEmailSearch search){
		Conditions condition = new Conditions();
		condition.and().likeBoth("t.title", search.getTitle());
		condition.and().is("t.type", search.getType());
		condition.and().is("t.status", search.getStatus());
		try {
			return insideEmailDao.countEntity(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public static InsideEmail getById(Long id){
		try {
			return insideEmailDao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 添加网站内部邮件
	 * @param email
	 * @param attachments
	 * @return
	 */
	public static boolean add(InsideEmail email,List<InsideEmailAttachment> attachments){
		if(attachments != null){
			email.setAttachmentCount(attachments.size());
		}else{
			email.setAttachmentCount(0);
		}
		try {
			if(email.getStatus() == null){
				email.setStatus(1);
			}
			email.setCreateDate(new Date());
			if(insideEmailDao.insert(email) > 0){
				if(attachments != null){
					for(InsideEmailAttachment item : attachments){
						item.setEmailId(email.getId());
						insideEmailAttachmentDao.insert(item);
					}
				}
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean update(InsideEmail email){
		try {
			if(insideEmailDao.updateNotNull(email) > 0){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 根据ID删除网站内部邮件
	 * @param id
	 * @return
	 */
	public static boolean deleteById(Long id){
		if(id != null){
			Conditions condition = new Conditions(InsideEmailAttachment.class);
			condition.and().is("emailId", id);
			try {
				//删除附件
				insideEmailAttachmentDao.deleteByCondition(condition);
				//删除邮件
				if(insideEmailDao.deleteById(id) > 0){
					return true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 获取邮件里所有附件
	 * @param emailId
	 * @return
	 */
	public static List<InsideEmailAttachment> findAttachmentByEmailId(Long emailId){
		if(emailId != null){
			Conditions condition = new Conditions(InsideEmailAttachment.class);
			condition.and().is("emailId", emailId);
			try {
				return insideEmailAttachmentDao.findByCondition(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static Long countAttachmentByEmail(Long emailId){
		if(emailId != null){
			Conditions condition = new Conditions(InsideEmailAttachment.class);
			condition.and().is("emailId", emailId);
			try {
				return insideEmailAttachmentDao.count(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0L;
	}

	public static boolean addAttachment(InsideEmailAttachment attachment){
		InsideEmail email = getById(attachment.getEmailId());
		if(email != null){
			try {
				if(insideEmailAttachmentDao.insert(attachment) > 0){
					if(email.getAttachmentCount() == null){
						email.setAttachmentCount(1);
					}else{
						email.setAttachmentCount(email.getAttachmentCount() + 1);
					}
					insideEmailDao.updateNotNull(email);
					return true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	/**
	 * 删除附件
	 * @param id
	 * @return
	 */
	public static boolean deleteAttachmentById(Long id){
		try {
			InsideEmailAttachment attachment = insideEmailAttachmentDao.getById(id);
			if(attachment != null){
				if(insideEmailAttachmentDao.deleteById(id) > 0){
					InsideEmail email = getById(attachment.getEmailId());
					if(email != null){
						email.setAttachmentCount(email.getAttachmentCount() - 1);
						if(email.getAttachmentCount() < 0){
							Long count = countAttachmentByEmail(email.getId());
							email.setAttachmentCount(count.intValue());
						}
						insideEmailDao.updateNotNull(email);
					}
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static InsideEmailUser getByEmailIdAndUserId(Long emailId,Long userId){
		if(emailId != null && userId != null){
			Conditions condition = new Conditions(InsideEmailUser.class);
			condition.and().is("emailId", emailId);
			condition.and().is("userId", userId);
			try {
				return insideEmailUserDao.get(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 发送网站内部邮件
	 * @param emailId
	 * @param users
	 * @return
	 */
	public static synchronized boolean send(Long emailId,List<? extends InsideEmailSendUser> users){
		if(emailId != null && users != null && users.size() > 0){
			//修改状态
			InsideEmail updateEmail = new InsideEmail();
			updateEmail.setId(emailId);
			updateEmail.setStatus(2);
			try {
				if(insideEmailDao.updateNotNull(updateEmail) > 0){
					for(InsideEmailSendUser user :users){
						InsideEmailUser rel = getByEmailIdAndUserId(emailId,user.getUserId());
						if(rel == null){
							rel = new InsideEmailUser();
							rel.setEmailId(emailId);
							rel.setUserId(user.getUserId());
							rel.setUserCode(user.getUserCode());
							rel.setStatus(InsideEmailUserEnum.Status.NotRead.index());
							try {
								insideEmailUserDao.insert(rel);
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
					return true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 查询用户网站内部邮件
	 * @param search
	 * @param pageNo
	 * @param pageLimit
	 * @return
	 */
	public static List<InsideEmailEntity> findUserEmailBySearch(InsideEmailSearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = new Conditions();
		condition.and().likeBoth("t.title", search.getTitle());
		condition.and().is("t.type", search.getType());
		condition.and().is("t1.user_id", search.getUserId());
		condition.and().is("t1.status", search.getStatus());
		condition.desc("t.create_date");
		try {
			return insideEmailUserDao.findEmailEntity(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 统计用户网站内部邮件数量
	 * @param search
	 * @return
	 */
	public static Long countUserEmailBySearch(InsideEmailSearch search){
		Conditions condition = new Conditions();
		condition.and().likeBoth("t.title", search.getTitle());
		condition.and().is("t.type", search.getType());
		condition.and().is("t1.user_id", search.getUserId());
		condition.and().is("t1.status", search.getStatus());
		try {
			return insideEmailUserDao.countEmailEntity(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	/**
	 * 修改用户网站内部邮件的状态
	 * @param emailId
	 * @param status
	 * @return
	 */
	public static boolean updateUserEmailStatus(Long userId,Long emailId,Integer status){
		Conditions condition = new Conditions(InsideEmailUser.class);
		condition.and().is("userId", userId);
		condition.and().is("emailId", emailId);
		if(status == 2){
			condition.and().is("status", 1);
		}
		InsideEmailUser rel = new InsideEmailUser();
		rel.setStatus(status);
		
		try {
			return insideEmailUserDao.updateNotNull(rel, condition) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除用户网站内部邮件
	 * @param userId
	 * @param emailId
	 * @return
	 */
	public static boolean deleteUserEmail(Long userId,Long emailId){
		InsideEmailUser rel = getByEmailIdAndUserId(emailId,userId);
		if(rel != null){
			if(rel.getStatus() == 3){
				//物理删除
				Conditions condition = new Conditions(InsideEmailUser.class);
				condition.and().is("userId", userId);
				condition.and().is("emailId", emailId);
				try {
					return insideEmailUserDao.deleteByCondition(condition) > 0;
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else{
				return updateUserEmailStatus(userId,emailId,3);
			}
		}
		return false;
	}
}
